//
//  DBParserRuleNamedInst.m
//  parser
//
//  Created by Daniel Braun on 26/05/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "DBParserRuleNamedInst.h"
#import "DBParser.h"
#import "DBLexer.h"

@implementation DBParserRuleNamedInst
+ (DBParserRuleNamedInst *) instRuleWith:(DBParserRule *)n
{
	NSAssert([n isKindOfClass:[DBParserRuleNamed class]], @"only DBParserRuleNamed should get instantiated");
	DBParserRuleNamedInst *inst=[[[[self class]alloc]initWithAction:(SEL) nil]autorelease];
	[inst setRuleDef:n];
	return inst;
}

- (DBParserRule *) ruleDef
{
	return ruledef;
}

- (void) setRuleDef:(DBParserRule *)r
{
	[ruledef release];
	ruledef=[r retain];
}

- (void) dealloc {
	[ruledef release];
	[super dealloc];
}

- (void) invokeWithParser:(DBParser *)p
{
	[self beginWithParser:p];
	[[ruledef nextSeq] invokeWithParser:p];
	[self matchedWithParser:p];
}

- (void) preProcess
{
	NSLog(@"preprocess for named %@\n", [ruledef name]);
	[[ruledef nextSeq] preProcess];
	[nextSeq preProcess];
}

- (int *) tokens
{
	return [[ruledef nextSeq] tokens];
}

- (void) matchedWithParser:(DBParser *)p consume:(BOOL)c
{
	[super matchedWithParser:p consume:c];
	NSLog(@"inst --- %@ }\n", [ruledef name]);
	[p astPop];
}

- (void) beginWithParser:(DBParser *)p {
	NSLog(@"{inst +++ %@\n", [ruledef name]);
	[super beginWithParser:p];
	[p astPush];
}

- (NSString *) description
{
	return [NSString stringWithFormat:@"RULE \"%@\"", [ruledef name]];
}

@end
